package fr.toto.logging;

/****************************************************************/
/* login */
/* Copyright 2006 Billy Anderson */
/****************************************************************/
import java.awt.*;
import java.awt.event.*;
import java.net.URI;
import java.util.Iterator;
import java.util.concurrent.Phaser;

import javax.swing.*;

import com.kokotchy.betaSeriesAPI.api.BetaseriesApiException;
import com.kokotchy.betaSeriesAPI.api.xmlImpl.BetaSerieApi;
import com.kokotchy.betaSeriesAPI.model.Error;

import fr.toto.constantes.Constantes;
import fr.toto.exceptions.ProcessException;

public class Login extends JFrame implements Constantes {
	/**
	 * 
	 */
	private static final long serialVersionUID = -13137731145742745L;
	// Variables declaration
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JTextField jTextField1;
	private JPasswordField jPasswordField1;
	private JButton jButton1;
	private JPanel contentPane;
	private final Phaser phaser;

	String username;
	String password;
	String token = null;

	// End of variables declaration
	public Login(Phaser phaser) {
		super();
		this.phaser = phaser;
		this.phaser.register();
		create();
		this.setVisible(true);
	}

	private void create() {
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jTextField1 = new JTextField(15);
		jPasswordField1 = new JPasswordField(15);
		jButton1 = new JButton();
		contentPane = (JPanel) this.getContentPane();
		//
		// jLabel1
		//
		jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabel1.setText(MESSAGES.getString("loginField"));
		//
		// jLabel2
		//
		jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabel2.setText(MESSAGES.getString("PasswordField"));
		//
		// jButton1
		//
		jButton1.setText(MESSAGES.getString("ConnexionButton"));
		jButton1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				jButton1_actionPerformed(e);
			}
		});
		//
		// jLabel3
		//
		JLabel jLabel3 = new JLabel(MESSAGES.getString("createAccountMsg"));
		jLabel3.setForeground(new Color(255, 0, 0));
		jLabel3.setFont(new Font("", Font.ITALIC, 12));
		jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
		jLabel3.addMouseListener(new MouseListener() {
			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseReleased(MouseEvent e) {
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				Desktop d = Desktop.getDesktop();
				try {
					d.browse(new URI(SubscribeURI));
				} catch (Exception e1) {
					new ProcessException(e1);
				}

			}
		});
		//
		// contentPane
		//
		contentPane.setLayout(new GridLayout(5, 1));
		JPanel pane1 = new JPanel();
		contentPane
				.add(new JLabel(MESSAGES.getString("logingMsg")));
		pane1.setLayout(new GridLayout(1, 2));
		pane1.add(jLabel1);
		pane1.add(jTextField1);
		contentPane.add(pane1);
		JPanel pane2 = new JPanel();
		pane2.setLayout(new GridLayout(1, 2));
		pane2.add(jLabel2);
		pane2.add(jPasswordField1);
		contentPane.add(pane2);
		contentPane.add(jButton1);
		contentPane.add(jLabel3);
		//
		// login
		//
		this.setTitle("Login");
		this.setSize(new Dimension(350, 120));
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setResizable(false);
	}


	private void jButton1_actionPerformed(ActionEvent e) {
		username = new String(jTextField1.getText());
		password = new String(jPasswordField1.getPassword());
		if (username.equals("") || password.equals("")) {
			jButton1.setEnabled(false);
			jLabel1.setForeground(new Color(0, 0, 0));
			jLabel2.setForeground(new Color(0, 0, 0));
			if (username.equals(""))
				jLabel1.setForeground(new Color(255, 0, 0));
			if (password.equals(""))
				jLabel2.setForeground(new Color(255, 0, 0));
			jButton1.setEnabled(true);
			this.setVisible(true);
		} else {
			jButton1.setEnabled(false);
			try {
				token = BetaSerieApi.getMembers().auth(username, password);
			} catch (BetaseriesApiException e1) {
				Iterator<Error> it = e1.getErrors().iterator();
				if (it.hasNext()) {
					Error er = it.next();
					switch (er.getCode()) {
					case 4002:
						jLabel1.setForeground(new Color(255, 0, 0));
						break;
					case 4003:
						jLabel2.setForeground(new Color(255, 0, 0));
						break;
					default:
						System.err.print(String.format(DEBUGMSG.getString("ApiException"), er.getCode(), er.getText() ));
						System.exit(-1);
					}
					token = null;
				}

			}
			if (token == null) {
				jLabel2.setForeground(new Color(255, 0, 0));
				jButton1.setEnabled(true);
				this.setVisible(true);
			} else {
				this.setVisible(false);
				phaser.arriveAndDeregister();
			}
		}
	}

	public String getUserName() {
		return username;
	}

	public String getToken() {
		return token;
	}

	public static void main(String[] args) {
		new Login(new Phaser());
	}

	public boolean failed() {
		if (token == null)
			return true;
		return false;
	}

}
